package com.hongsuan.pplive.mobileAPI.pay;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.util.AlipayNotify;
import com.cnjson.common.config.Global;
import com.cnjson.common.web.BaseController;
import com.hongsuan.pplive.mobileAPI.utils.JsonUtils;
import com.hongsuan.pplive.mobileAPI.vo.Status;
import com.hongsuan.pplive.modules.live.entity.LiveRechargeRecord;
import com.hongsuan.pplive.modules.live.service.LiveRechargeRecordService;
import com.hongsuan.pplive.modules.sys.entity.User;
import com.hongsuan.pplive.modules.sys.service.SystemService;

import me.chanjar.weixin.common.util.StringUtils;

@Controller
@RequestMapping("/pay/aplipay")
public class AlipayApiController extends BaseController {

	@Autowired
	private LiveRechargeRecordService liveRechargeRecordService;

	@Autowired
	private SystemService systemService;

	/*
	 * @RequestMapping(value = "/doPay", method = RequestMethod.POST, produces =
	 * "application/json") public String doPay(HttpServletRequest request,
	 * HttpServletResponse response, Model model) throws Exception { //
	 * PrintWriter out = response.getWriter(); String result = ""; Date date =
	 * new Date(); // 支付类型 // 必填，不能修改 String payment_type = "1"; // 服务器异步通知页面路径
	 * // 需http://格式的完整路径，不能加?id=123这类自定义参数 String notify_url =
	 * "http://119.29.166.150/v2/aplipay/async"; // 页面跳转同步通知页面路径 //
	 * 需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/ String return_url
	 * = "http://119.29.166.150/v2/aplipay/return_url"; // 商户订单号. //
	 * 商户网站订单系统中唯一订单号，必填 // String out_trade_no = date.getTime() + ""; // 订单名称
	 * // 必填 String subject = "充值测试"; // 防钓鱼时间戳 //
	 * 若要使用请调用类文件submit中的query_timestamp函数 String anti_phishing_key = ""; //
	 * 客户端的IP地址 // 非局域网的外网IP地址，如：221.0.0.1 String exter_invoke_ip = "";
	 * 
	 * String total_fee = ServletRequestUtils.getStringParameter(request,
	 * "amount", "");
	 * 
	 * String body = ServletRequestUtils.getStringParameter(request, "body",
	 * "test"); // 商品展示地址 String show_url =
	 * ServletRequestUtils.getStringParameter(request, "show_url",
	 * "http://www.elve.cn"); //
	 * 需以http://开头的完整路径，例如：http://www.xxx.com/myorder.html
	 * 
	 * Map<String, String> sParaTemp = new HashMap<String, String>();
	 * sParaTemp.put("service", "create_direct_pay_by_user");// 接口服务----即时到账
	 * sParaTemp.put("partner", AlipayConfig.partner);// 支付宝PID
	 * sParaTemp.put("_input_charset", AlipayConfig.input_charset);// 统一编码
	 * sParaTemp.put("payment_type", payment_type);// 支付类型
	 * sParaTemp.put("notify_url", notify_url);// 异步通知页面
	 * sParaTemp.put("return_url", return_url);// 页面跳转同步通知页面
	 * sParaTemp.put("seller_email", Constants.SELLER_EMAIL);// 卖家支付宝账号
	 * sParaTemp.put("out_trade_no", date.getTime() + payment_type);// 商品订单编号
	 * sParaTemp.put("subject", subject);// 商品名称 sParaTemp.put("total_fee",
	 * total_fee);// 价格 sParaTemp.put("body", body); sParaTemp.put("show_url",
	 * show_url); sParaTemp.put("anti_phishing_key", anti_phishing_key);
	 * sParaTemp.put("exter_invoke_ip", exter_invoke_ip);
	 * 
	 * // 建立请求 try { String sHtmlText = AlipaySubmit.buildRequest(sParaTemp,
	 * "post", "确认"); Gson gson = new GsonBuilder().setDateFormat(
	 * "yyyy-MM-dd HH:mm:ss").create(); String s = gson.toJson(sHtmlText);
	 * model.addAttribute("sHtmlText", s); request.setAttribute("sHtmlText", s);
	 * result = "{\"success\":true,\"msg\":\"跳转成功\"}";
	 * StringUtil.writeToWeb(sHtmlText, "html", response); return null; } catch
	 * (Exception e) { if (log.isErrorEnabled()) {
	 * log.error("ddddddddddddddddddddd"); } result =
	 * "{\"success\":false,\"msg\":\"跳转失败，请稍候再试！\"}";
	 * StringUtil.writeToWeb(result, "html", response); return null; } }
	 * 
	 *//**
		 * 同步通知的页面的Controller
		 * 
		 * @param request
		 * @param response
		 * @return
		 *//*
		 * @RequestMapping(value = "/return_url") public String
		 * Return_url(HttpServletRequest request, HttpServletResponse response)
		 * { return "web/pay/success"; }
		 */

	/**
	 * 异步通知付款状态的Controller
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/asyncNotify", method = RequestMethod.GET)
	@ResponseBody
	public String async(HttpServletRequest request, HttpServletResponse response) {
		Status status = new Status();
		try {
			status.setCode(1);
			status.setMessage(Global.FAILED);
			Map<String, String> params = new HashMap<String, String>();
			Map<?, ?> requestParams = request.getParameterMap();
			for (Iterator<?> iter = requestParams.keySet().iterator(); iter.hasNext();) {
				String name = (String) iter.next();
				String[] values = (String[]) requestParams.get(name);
				String valueStr = "";
				for (int i = 0; i < values.length; i++) {
					valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
				}
				params.put(name, valueStr);
			}
			String tradeNo = request.getParameter("out_trade_no");// 订单号
			String tradeStatus = request.getParameter("trade_status");// 交易状态
			// String notifyId = request.getParameter("notify_id");

			LiveRechargeRecord record = liveRechargeRecordService.get(tradeNo);
			//record.setUpdateDate(new Date());
			// 验证成功
			if (AlipayNotify.verify(params)) {
				if (tradeStatus.equals("TRADE_FINISHED") || tradeStatus.equals("TRADE_SUCCESS")) {
					String strPrice = request.getParameter("price");// 金额
					if (record != null && record.getCreateById() != null) {
						record.setState(2);
						Float money = record.getAmount();
						if (!StringUtils.isEmpty(strPrice)) {
							money = Float.parseFloat(strPrice);
						}
						User user = systemService.getUser(record.getCreateById());
						user.setAccount(user.getAccount() + money);
						systemService.updateUserAccount(user);
						status.setCode(0);
						status.setMessage(Global.SUCCESS);
						logger.info(">>>>>充值成功" + tradeNo);
					}

				} else {
					record.setState(3);
					status.setCode(1);
					status.setMessage(tradeStatus);
					logger.info(">>>>>交易失败" + tradeNo);
				}
			} else {
				record.setState(3);
				status.setCode(2);
				status.setMessage(tradeStatus);
				logger.info(">>>>>访问验证失败" + tradeNo);
			}
			liveRechargeRecordService.save(record);

		} catch (Exception e) {
			logger.error(e.getMessage());
			status.setCode(10);
			status.setMessage(Global.FAILED);
		}
		return JsonUtils.toJson(status);
	}

}
